<?php

namespace App\Http\Controllers;

use App\Http\Helpers\StatusResponse;
use App\Models\Blog;
use App\Models\Comment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    /**
     * 评论
     * 使用该评论方法将会自动创建账户，如果您的账户存在则会要求您登录，
     * @return JsonResponse
     */
    function pushComment ($target) : JsonResponse
    {
        // 是否匿名
        $anonymous = \request('anonymous');
        // 评论内容
        $content = \request('content');
        // 评论用户 默认为NULL
        $user = null;
        // 回复的评论
        $reply = \request('reply_id');
        // 评论的ip
        $ip = '0.0.0.0';
        // 评论的状态
        $status = 0;
        // 加入用户信息
        if (!$anonymous) {
            if (!$user_info = $this->getUserInfo()) {
                return $this->failWithArr(StatusResponse::PERMISSION_REQUIRE);
            }
            $user = $user_info->id;
        }
        // 检查评论的文章是否有效
        if (!$blog = Blog::query()->find($target)) {
            return $this->failWithArr(StatusResponse::NOT_FOUND);
        }
        // 将blog的评论数加一
        $blog->increment('comment_count');
        // 检查回复的目标是否存在
        if (!$reply) {
            if (Comment::query()->find($reply)) {
                return $this->failWithArr(StatusResponse::NOT_FOUND);
            }
        }
        // 评论内容审查
        // -------
        // 发布评论
        if (Comment::query()->create([
            'reply_id' => $reply,
            'author_id' => $user,
            'blog_id' => $target,
            'comment' => $content,
            'ip' => $ip,
            'status' => $status
        ])) {
            return $this->success();
        }
        return $this->unknown_err();
    }

    /**
     * 获取目标的所有评论
     * @param $id
     * @return JsonResponse
     */
    public function getBlogComment ($id) : JsonResponse
    {
        $arr = Comment::query()
            ->with(['author', 'replies'])
            ->where('blog_id', $id)
            ->where('reply_id', null)
            ->where('status', 0)
            ->get();
        return $this->success($arr);
    }

    /**
     * 获取无层级的评论列表
     * @param $id
     * @return JsonResponse
     */
    public function getNoFormatBlogComment ($id) : JsonResponse
    {
        if (!$this->isAdmin()) {
            return $this->failWithArr(StatusResponse::PERMISSION_REQUIRE);
        }
        return $this->success(Comment::query()
            ->with(['author', 'reply'])
            ->where('blog_id', $id)
            ->get());
    }

    /**
     * 删除评论
     * @param $id
     * @return JsonResponse
     */
    public function deleteComment ($id) : JsonResponse
    {
        if (!$comment = Comment::query()->find($id))
        {
            return $this->failWithArr(StatusResponse::NOT_FOUND);
        }
        if (!$this->isAdminOrSelf($comment->author_id))
        {
            return $this->failWithArr(StatusResponse::PERMISSION_REQUIRE);
        }
        if ($comment->delete())
        {
            // 处理评论数
            if (!$blogs = Blog::query()->find($comment->blog_id)) {
                return $this->failWithArr(StatusResponse::NOT_FOUND);
            }
            if (!$blogs->decrement('comment_count')) {
                return $this->unknown_err();
            }
            return $this->success();
        }
        return $this->unknown_err();
    }

    /**
     * 状态改变
     * @param $id
     * @return JsonResponse
     */
    public function statusChange ($id) : JsonResponse
    {
        $status = \request('status');
        if (!$this->isAdmin()) {
            return $this->failWithArr(StatusResponse::PERMISSION_REQUIRE);
        }
        if (!$comment = Comment::query()->find($id)) {
            return $this->failWithArr(StatusResponse::NOT_FOUND);
        }
        if ($comment->status == 2) {
            return $this->failWithArr(StatusResponse::IRREVERSIBLE_OPERATION);
        }
        if ($comment->update(['status' => $status])) {
            return $this->success();
        }
        return $this->unknown_err();
    }
}
